<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            3D化学后台管理系统
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="./css/x-admin.css" media="all">
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>模型管理</cite></a>
              <a><cite>模型列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </div>
        <div class="x-body">
            <form id="form-query" class="layui-form x-center" action="">
                <div class="layui-form-pane" style="margin-top: 15px;">
                  <div class="layui-form-item">
                    <label class="layui-form-label">所属分类</label>
                    <div class="layui-input-inline">
                        <select id="query-modelType" name="modelType" style="text-align: left">
                            <option value="">请选择分类</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <input id="query-modelName" type="text" name="modelName"  placeholder="请输入有机物名称" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button id="btn-query" class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                    </div>
                  </div>
                </div> 
            </form>
            <xblock>
                <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button>
                <button class="layui-btn" onclick="model_add('添加模型','model-add.html','','','600','600')"><i class="layui-icon">&#xe608;</i>添加</button>
                <span id="span-count" class="x-right" style="line-height:40px">共有数据：0 条</span>
            </xblock>
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" name="" value="">
                        </th>
                        <th style="width: 30px;">
                            ID
                        </th>
                        <th>
                            有机物名称
                        </th>
                        <th>
                            分类
                        </th>
                        <th>
                            模型图片
                        </th>
                        <th style="width: 40%;">
                            有机物详情
                        </th>
                        <th>
                            收藏人数
                        </th>
                        <th>
                            SMILES
                        </th>
                        <th style="width: 60px;">
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody id="model-list">
                    <tr style="display: none;">
                        <td id="td-check">
                            <input type="checkbox" value="1" name="">
                        </td>
                        <td>
                            1
                        </td>
                        <td>
                            甲烷
                        </td>
                        <td>
                            烷烃
                        </td>
                        <td>
                            <img  src="" width="100" alt="">
                        </td>
                        <td>
                            详情
                        </td>
                        <td>
                            0
                        </td>
                        <td>
                            SMILES
                        </td>
                        <td id="td-manage" class="td-manage">
                            <a title="编辑" href="javascript:;" onclick="model_edit(event,'编辑','model-edit.html','','','600','600')"
                            class="ml-5" style="text-decoration:none">
                                <i class="layui-icon">&#xe642;</i>
                            </a>
                            <a title="删除" href="javascript:;" onclick="model_del(event)" 
                            style="text-decoration:none">
                                <i class="layui-icon">&#xe640;</i>
                            </a>
                        </td>
                    </tr>
                </tbody>
            </table>

            <div id="page" class="layui-laypage"></div>

        </div>
        <script src='./js/jquery.min.js' type='text/javascript' ></script>
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script src="./js/x-layui.js" charset="utf-8"></script>
        <script src="http-config.js"></script>

        <script>  
            layui.use(['element','layer','form','laypage'], function(){
                $ = layui.jquery;//jquery
              lement = layui.element();//面包导航
              layer = layui.layer;//弹出层
              form = layui.form();
              laypage = layui.laypage;

              layer.ready(function(){ //为了layer.ext.js加载完毕再执行
                  layer.photos({
                    photos: '#model-list'
                    ,shift: 0 //0-6的选择，指定弹出图片动画类型，默认随机
                  });
                });

              
              //选择器
              $.ajax({
                url: baseURL + "/model_type/list",
                type: "GET",
                dataType: "json",
                success: function(data) {
                    var select = $("select[name='modelType']");
                    var newOption = $('<option>', {
                            value: '全部分类',
                            text: '全部分类'
                        });
                        
                        // 向 <select> 元素中添加新的 <option> 元素
                        select.append(newOption);
                    $.each(data, function(index, item) {
                        // 创建新的 <option> 元素
                        var newOption = $('<option>', {
                            value: item.typeName,
                            text: item.typeName
                        });
                        
                        // 向 <select> 元素中添加新的 <option> 元素
                        select.append(newOption);
                    });

                    // 重新渲染 select 元素
                    form.render('select');
                },
                error: function(xhr, status, error) {
                    console.log(xhr.responseText);
                }
            });

              //分页器
            $.ajax({// 发送网络请求获取数据
                    url: baseURL + "/model_info/list_page",
                    type: 'GET',
                    success: function (data) {
                        var totalPages = data.totalPages;
                        var totalElements = data.totalElements;
                        var pageSize = data.size;
                        var currentPage = data.number + 1;
                        
                        document.getElementById("span-count").innerHTML = '共有数据：' + totalElements + ' 条'

                        var pageConfig = {
                        cont: 'page', // 分页容器的 ID
                        pages: totalPages, // 总页数
                        limit: pageSize, // 每页显示的记录数
                        curr: currentPage, // 当前页码
                        jump: function (obj, first) {
                            if (!first) {
                                currentPage = obj.curr;
                                $.ajax({
                                    url:baseURL + "/model_info/list_page?pageNo=" + currentPage,
                                    contentType:"application/json; charset=utf-8",
                                    success:function (data){
                                    showlist(data.content);
                                    }
                                })
                            }
                        }
                        };
                        
                        // 渲染分页器
                        laypage(pageConfig);
                        showlist(data.content);
                    },
                    error: function () {
                        layer.msg('网络请求发生错误');
                    }
                });


            })

            //批量删除提交
            function delAll () {
                layer.confirm('确认要删除吗？',function(index){
                    //捉到所有被选中的，发异步进行删除
                     var tbody = document.getElementById("model-list");
                    var rows = tbody.getElementsByTagName("tr");
                    for(let i = 1; i < rows.length; i++){
                        var modelId = rows[i].cells[1].textContent;  // 获取第二个单元格的文本内容
                        var checked = rows[i].cells[0].querySelector('input[type="checkbox"]').checked;  // 获取第二个单元格的文本内容
                        
                        if(checked){
                            $.ajax({
                                url:baseURL + "/model_info/deleteModelInfoByModelId/" + modelId,
                                contentType:"application/json; charset=utf-8",
                                success:function (data){
                                    console.log(data);
                                }
                            });
                            console.log(i);
                        }
                    }
                    //捉到所有被选中的
                    location.reload();//刷新父页面
                    layer.msg('删除成功', {icon: 1});
                });
             }

            function model_add(title,url,id,name,w,h){
                x_admin_show(title,url,id,name,w,h);
            }

             //-编辑
            function model_edit (event,title,url,id,name,w,h) {
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var modelId = row.cells[1].textContent;  // 获取第二个单元格的文本内容
                var modelName = row.cells[2].textContent;
                var modelType = row.cells[3].textContent;
                var modelDescription = row.cells[5].textContent;
                var collectionCount = row.cells[6].textContent;
                var keyLine = row.cells[7].textContent;
                layer.open({
                    type: 2,
                    area: [w+'px', h +'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade:0.4,
                    title: title,
                    content: url+
                    '?modelId=' + modelId + 
                    '&modelName=' + modelName + 
                    '&modelType=' + modelType + 
                    '&modelDescription=' + modelDescription + 
                    '&collectionCount=' + collectionCount + 
                    '&keyLine=' + keyLine
                });
            }
           
            /*-删除*/
            function model_del(event){
                var button = event.target;  // 获取当前点击的按钮元素
                var row = button.parentElement.parentElement.parentElement;  // 获取该行的元素
                var modelId = row.cells[1].textContent;  // 获取第二个单元格的文本内容

                layer.confirm('确认要删除吗？',function(index){
                    //发异步删除数据
                    $.ajax({
						url:baseURL + "/model_info/deleteModelInfoByModelId/" + modelId,
						contentType:"application/json; charset=utf-8",
						success:function (data){
                           console.log(data);
						},
                        error:function(data){
                            console.log(data);
                        }
					});
                    row.remove();
                    location.reload();//刷新父页面
                    layer.msg('已删除!',{icon:1,time:1000});
                });
            }

            /*分类查询*/
            $('#form-query').submit(function(event){
                // 阻止表单默认的提交行为
                event.preventDefault();

                const modelType = $("#query-modelType").val();
                const modelName = $("#query-modelName").val();
                console.log(modelType);

                var url = baseURL + "/model_info/findModelInfosByModelTypeAndModelName?modelType=" + modelType + "&modelName=" + modelName;
                $.ajax({
                    url: url,
                    type: 'GET',
                    contentType:"application/json; charset=utf-8",
                    success:function(data){
                        var totalPages = data.totalPages;
                        var totalElements = data.totalElements;
                        var pageSize = data.size;
                        var currentPage = data.number + 1;
                                
                        document.getElementById("span-count").innerHTML = '共有数据：' + totalElements + ' 条'

                        var pageConfig = {
                            cont: 'page', // 分页容器的 ID
                            pages: totalPages, // 总页数
                            limit: pageSize, // 每页显示的记录数
                            curr: currentPage, // 当前页码
                            jump: function (obj, first) {
                                if (!first) {
                                    currentPage = obj.curr;
                                    $.ajax({
                                        url:url + "&pageNo=" + currentPage,
                                        contentType:"application/json; charset=utf-8",
                                        success:function (data){
                                        showlist(data.content);
                                        }
                                    })
                                }
                            }
                        };
                                
                        // 渲染分页器
                        laypage(pageConfig);
                        showlist(data.content);
                    },error:function(data){
                        console.log(data);
                    }
                });
            });

            /*展示表格*/
            function showlist(data){
                //提前获取数据
                var checkinnerHTML = document.getElementById('td-check').innerHTML;
                var manageinnerHTML = document.getElementById('td-manage').innerHTML;

                // 获取tbody
                var tbody = document.getElementById("model-list");
                var rows = tbody.getElementsByTagName("tr");
                for(var i = 0; i < rows.length; i++){
                    rows[i].style.display = "none";
                }

                for(var i = 0; i < data.length; i++){
                    // 创建一个行元素
                    var row = document.createElement('tr');
                    // 创建td单元格 
                    var check = document.createElement('td'); check.id = "td-check";
                    var modelId = document.createElement('td');
                    var modelName = document.createElement('td');
                    var modelType = document.createElement('td');
                    var modelImg = document.createElement('td');
                    var modelDescription = document.createElement('td');
                    var collectionCount = document.createElement('td');
                    var keyLine = document.createElement('td');
                    var manage = document.createElement('td'); manage.id = "td-manage";

                    //显示数据
                    check.innerHTML = checkinnerHTML;
                    modelId.innerHTML = data[i].modelId;
                    modelName.innerHTML = data[i].modelName;
                    modelType.innerHTML = data[i].modelType;
                    modelImg.innerHTML = '<img  src="'+ baseURL+data[i].modelImg +'" width="100" alt="">';
                    modelDescription.innerHTML = data[i].modelDescription;
                    collectionCount.innerHTML = data[i].collectionCount;
                    keyLine.innerHTML = data[i].keyLine;
                    manage.innerHTML = manageinnerHTML;            

                    // 将新建的td加入新建的tr行
                    row.appendChild(check);
                    row.appendChild(modelId);
                    row.appendChild(modelName);
                    row.appendChild(modelType);
                    row.appendChild(modelImg);
                    row.appendChild(modelDescription);
                    row.appendChild(collectionCount);
                    row.appendChild(keyLine);
                    row.appendChild(manage);
                        
                    // 将这行加到tbody
                    tbody.appendChild(row);
                }
            }

            </script>
            <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
          var s = document.getElementsByTagName("script")[0]; 
          s.parentNode.insertBefore(hm, s);
        })();
        </script>
    </body>
</html>